package com.qinghuan.threads.chapterOne;

/**
 * @Author: dubao
 * @Date: 2022/5/17 19:42
 * @Description:
 * 当一个线程调用yield方法时，当前线程会让出CPU使用权，然后处于就绪状态，线程调度器会从线程就绪队列中随机获取一个线程，
 * 如果设置了优先级，优先级高的被调用的几率更大
 */
public class Test1_6_1 implements Runnable {

    Test1_6_1() {
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        for (int i=0; i<5; i++) {
            //当i==0时，让出CPU执行权，放弃时间片，进行下一轮调度
            if (i % 5 ==0) {
                System.out.println(Thread.currentThread() + "yield cpu...");
                Thread.yield();

            }
        }
        System.out.println(Thread.currentThread() + " is over");
    }

    public static void main(String[] args) {
        new Test1_6_1();
        new Test1_6_1();
        new Test1_6_1();
    }
}
